// pages/myPages/contactsPage/addContactsPage/addContactsPage.js
var request = require('./request.js')
//console.log(request)
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName:'',
    WalletAddress:'',
    MobilePhoneNo:'',
    email:'',
    note:'',
    showView: false,
    WalletType:'BTC',
    coinType:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getcoinType && this.getcoinType()
  },
  onChangeShowState: function (e) {
    console.log(e)
    var that = this;
    var id = e.currentTarget.dataset.id
    if(!id){
      var current = e.currentTarget.dataset.current;
      this.setData({
        WalletType: current
      })
    }
    that.setData({
      showView: (!that.data.showView)
    })
  },
  addContactsFinish: function (e) {
    this.addContact();
    // wx.navigateBack({
    //   delta: 1
    // })
  },
  nameInputEvent: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },
  mobileInputEvent: function (e) {
    this.setData({
      WalletAddress: e.detail.value
    })
  },
  PhoneNoInputEvent: function (e) {
    this.setData({
      MobilePhoneNo: e.detail.value
    })
  },
  emailInputEvent: function (e) {
    let email = e.detail.value
    let checkedNum = this.checkEmail(email)
  },
  checkEmail: function (email) {
    let str = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/
    if (str.test(email)) {
      this.setData({
        email: email
      })
      return true
    } else {
      this.setData({
        email: ''
      })
      wx.showToast({
        title: '请填写正确的邮箱号',
        title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].correctEmail : wx.T.locales['en'].correctEmail,
        icon: 'none',
        duration: 2000,
        mask: true
      })
      return false
    }
  },
  noteInputEvent: function (e) {
    this.setData({
      note: e.detail.value
    })
  },
  addContact: function (e) {
    //console.log(e)
    var that = this;
    var data = {
      access_token: wx.getStorageSync('createUser').access_token,
      name: that.data.userName,
      wallet_addr:that.data.WalletAddress,
      mobile:that.data.MobilePhoneNo,
      coin_symbol: that.data.WalletType,
      email:that.data.email,
      remark:that.data.note
    };
    request.contactAdd(data, function (d) {
      console.log(d)
      wx.showToast({
        title: d.message,
        icon: 'succes',
        duration: 500,
        mask: true,
        success:function(){
          if (d.code == 200) {
            wx.navigateBack({
              delta: 1
            })
          }
        }
      })

     
    });
  },
  saoyisao: function (e) {
    console.log(1)
    wx.scanCode({
      success: (res) => {
        console.log(res.result)
        this.setData({
          WalletAddress: res.result
        })
      }
    })
  },
  getcoinType(){
    let $this = this
    request.GetCoinType({},res=>{
      $this.setData({
        coinType:res.data
      })
      //console.log(res.data)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      setLang: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'] : wx.T.locales['en']

    })
    wx.setNavigationBarTitle({
      title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].NewContact : wx.T.locales['en'].NewContact
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})